package com.example.haojiutao.controller.user;

import com.example.haojiutao.common.Result;
import com.example.haojiutao.model.domain.Address;
import com.example.haojiutao.model.domain.User;
import com.example.haojiutao.service.AddressService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 地址控制器
 * @Author: cheng_jianzhen
 * @CreateTime: 2024/1/29 19:24
 */
@RestController
@Slf4j
public class AddressContorller {
    @Autowired
    private AddressService addressService;
    /*
     * @Description: 用户添加地址
     * @Author: cheng_jianzhen
     * @CreateTime: 2024/1/29 19:46
     * @param: address
     * @return: com.example.treasure_box.common.Result
     **/
    @PostMapping("address")
    public Result insertAdd(@RequestBody Address address){

        log.info("========添加地址=========",address);

        if(address.getName() != null && !"".equals(address.getName())
                && address.getPhoneNum() != null && !"".equals(address.getPhoneNum())){//收货人和手机号不能为空
            try{
                 addressService.insertAddress(address);

            }catch (Exception e){
                throw new RuntimeException("新增地址信息失败:"+e.getMessage());
            }
        }else{
            throw new RuntimeException("收货人和手机号不能为空！");
        }

        addressService.insertAddress(address);
        return Result.success("添加地址成功");

    }
    /*
     * @Description: 修改地址信息
     * @Author: cheng_jianzhen
     * @CreateTime: 2024/1/29 19:58
     * @param: address
     * @return: com.example.treasure_box.common.Result
     **/
    @PutMapping("/address/info")
    public int updateInfo(@RequestBody Address address){
        log.info("=========待修改的地址信息========" ,address);

        address.setPhoneNum(address.getPhoneNum());
        address.setName(address.getName());
        address.setAddress(address.getAddress());

        return addressService.updateAddress(address);

    }
    /*
     * @Description: 删除地址
     * @Author: cheng_jianzhen
     * @CreateTime: 2024/1/29 20:06
     * @param: null
     * @return: null
     **/

//
//    @DeleteMapping("/address/delete")
//    public int delete(Integer id){
//
//        return addressService.deleteAddress(id);
//    }
    @DeleteMapping("/address/{id}")
    public int delete1(@PathVariable("id") Integer id){

        return addressService.deleteAddress(id);
    }
    /*
     * @Description: 根据ID查询地址
     * @Author: cheng_jianzhen
     * @CreateTime: 2024/1/29 20:19
     * @param: id
     * @return: com.example.treasure_box.common.Result
     **/
    @GetMapping("address/info/{id}")
    public Result selectById(@PathVariable("id") Integer id){
        Address address=addressService.selectById(id);

        if(address!=null){
            return Result.success("查询成功");
        }else {
            return Result.faile("没有找到对象id");
        }

    }
    /*
     * @Description: 查询用户所有地址
     * @Author: cheng_jianzhen
     * @CreateTime: 2024/1/29 20:27
     * @param: address
     * @return: com.example.treasure_box.common.Result
     **/
    @GetMapping("address/list")
    public Result selectList(Address address){
        log.info("列表查询参数 ：{}" ,address);
        List<Address> addresses =addressService.selectList(address);
        PageInfo<Address> pageInfo=new PageInfo<>(addresses);
        return Result.success("查询成功",address);
    }


}
